import time

import requests

# 获取列表
def get_noval_list():
    # 接口的URL
    url = 'https://m.uaa.com/api/novel/app/novel/search?keyword=&orderType=0&page=1&size=10'

    # 发送GET请求
    response = requests.get(url)

    # 检查响应状态
    if response.status_code == 200:
        # 获取响应内容（假设是JSON格式）
        data = response.json()
        return data['model']['data']
    else:
        print('Failed to retrieve data: ', response.status_code)

# 获取目录
def get_catalog_list(noval_id):
    # 接口的URL
    url = 'https://m.uaa.com/api/novel/app/novel/catalog/'+noval_id

    # 发送GET请求
    response = requests.get(url)

    # 检查响应状态
    if response.status_code == 200:
        # 获取响应内容（假设是JSON格式）
        data = response.json()
        return data['model']['menus']
    else:
        print('Failed to retrieve data: ', response.status_code)

def get_content(chapter_id):
    # 接口的URL
    view_id = timestamp = int(time.time())
    url = 'https://m.uaa.com/api/novel/app/novel/chapter?id='+chapter_id+'&offset=0&viewId=' + str(view_id) + '000333'
    print(url)

    # 发送GET请求
    response = requests.get(url)

    # 检查响应状态
    if response.status_code == 200:
        # 获取响应内容（假设是JSON格式）
        data = response.json()
        return data['model']['lines']
    else:
        print('Failed to retrieve data: ', response.status_code)
if __name__ == '__main__':
    noval_list = get_noval_list()
    for i in range(9, 10):
        print('书籍：', noval_list[i]['title'])
        catalog_list = get_catalog_list(noval_list[i]['id'])
        for j in range(len(catalog_list)):
            print("章节：", catalog_list[j]['title'])
            if len(catalog_list[j]['children'])>0:
                for k in range(len(catalog_list[j]['children'])):
                    content = get_content(catalog_list[j]['children'][k]['id'])
                    print('\n'.join(content))
            else:
                content = get_content(catalog_list[j]['id'])
                print('\n'.join(content))
    # print('书籍：',noval_list[9])
    # catalog_list = get_catalog_list(noval_list[9]['id'])
    # print('章节：', catalog_list[0])
    # if len(catalog_list[0]['children'])>0:
    #      content = get_content(catalog_list[0]['children'][0]['id'])
    # else:
    #     content = get_content(catalog_list[0]['id'])
    # # get_content('896633419402973184')
    # print('内容：', content)